package com.springmvc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springmvc.model.Test;
import com.springmvc.service.ITestService;
import com.springmvc.test.SendGetTest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiehui on 2018/4/25.
 */
@Controller
@RequestMapping("/test")
public class TestController {

    @Resource
    private ITestService testService;

    @RequestMapping("/index_page")
    public String showIndex(HttpServletRequest request, Model model) {
        int id = Integer.parseInt(request.getParameter("id"));
        Test test = this.testService.getModelById(id);
        //绑定 d---对象到test/index.jsp
        model.addAttribute("test", test);
        return "test/index";
    }

    @RequestMapping("/index_api")
    @ResponseBody
    public Test Index(HttpServletRequest request,Model model) {
        int id = Integer.parseInt(request.getParameter("id"));
        return this.testService.getModelById(id);
    }
    @RequestMapping(value="list")
    public ModelAndView ConfigurationList(@RequestParam(value="pageNum",required=false, defaultValue="1")Integer pageNum){
       ModelAndView mv = new ModelAndView();
        PageHelper.startPage(pageNum, 10);
        List<Test> list =testService.selectTestList();
        PageInfo<Test> pagehelper = new PageInfo<Test>(list);
        mv.addObject("pagehelper",pagehelper);
        mv.setViewName("test/testList");
        return mv;
    }
    @RequestMapping(value="user")
    @ResponseBody
    public Map<String,String> test(HttpServletRequest request,String username,String password,String membertype){
        Map<String,String> result = new HashMap<>();
        String url = "http://test.chenjianping.com/Api/loginCheck";
        String param = "username/15080011640/password/12346/membertype/1";
        String msg = SendGetTest.sendGet(url,param);
       result.put("returnCode","000000");
       result.put("msg",msg);
        return result;
    }
}